package com.feisuanyz.service.validation.impl;

import com.feisuanyz.dto.EventValidationQuery;
import com.feisuanyz.dto.RestResult;
import com.feisuanyz.entity.EventManagement;
import com.feisuanyz.repository.EventManagementRepository;
import com.feisuanyz.service.validation.EventValidationService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 事件验证服务实现类
 */
@Service
@Slf4j
public class EventValidationServiceImpl implements EventValidationService {

    @Autowired
    private EventManagementRepository eventManagementRepository;

    @Override
    public RestResult validateEvent(EventValidationQuery query, String operation) {
        if ("edit".equalsIgnoreCase(operation) || "delete".equalsIgnoreCase(operation)) {
            if (query.getEventId() == null) {
                return new RestResult("000001", "事件ID不能为空", null);
            }
            EventManagement event = eventManagementRepository.findById(query.getEventId()).orElse(null);
            if (event == null) {
                return new RestResult("000001", "事件ID不存在", null);
            }
        }

        if (query.getTitle().length() > 255) {
            return new RestResult("000001", "事件标题不能超过255个字符", null);
        }

        if (query.getLocation() != null && query.getLocation().length() > 255) {
            return new RestResult("000001", "事件地点不能超过255个字符", null);
        }

        if (query.getVenue() != null && query.getVenue().length() > 255) {
            return new RestResult("000001", "事件场地不能超过255个字符", null);
        }

        if (query.getEventDate() != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                dateFormat.parse(dateFormat.format(query.getEventDate()));
            } catch (ParseException e) {
                return new RestResult("000001", "事件日期格式无效", null);
            }
        }

        if (query.getEventTime() != null) {
            SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
            try {
                timeFormat.parse(query.getEventTime());
            } catch (ParseException e) {
                return new RestResult("000001", "事件时间格式无效", null);
            }
        }

        return new RestResult("000000", "调用成功", null);
    }
}